package edu.aynu.service.impl;

import edu.aynu.entity.Choice;
import edu.aynu.entity.Cloze;
import edu.aynu.es.EsClozeDao;
import edu.aynu.service.EsClozeService;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 28241
 * @Date 2021/5/24 17:17
 */
@Service
public class EsClozeServiceImpl implements EsClozeService {

    @Autowired
    private EsClozeDao esClozeDao;

    /**
     * 通过关键字在cloze索引中查询
     * @param cloze
     * @return
     */
    @Override
    public List<Cloze> queryBySubject(Cloze cloze) {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("subject", cloze.getSubject());
        List<Cloze> clozes = new ArrayList<>();
        esClozeDao.search(matchQueryBuilder).forEach(clozeTemp -> clozes.add(clozeTemp));
        return clozes;
    }
}
